package com.yxjx.duoxue.comment;

import java.util.ArrayList;

import com.yxjx.duoxue.R;
import com.yxjx.duoxue.customview.YxGridView;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AllThumbnailsView extends LinearLayout {
	
	public static final String KEY_EXTRA_IMAGES = "KEY_EXTRA_IMAGES";
	public static final String KEY_EXTRA_INDEX = "KEY_EXTRA_INDEX";

	private View mRoot;
	private YxGridView mGridView;
	private GridAdapter mAdapter;
	private String mImageUrls;
	private ArrayList<String> mCorrectedUrls;
	private String lastImageTipText = "";
	
	/**
	 * used by school intro
	 * @param text
	 */
	public void setLastImageTipText(String text) {
		lastImageTipText = text;
	}

	public AllThumbnailsView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		mRoot = LayoutInflater.from(context).inflate(R.layout.segment_all_thumbnails, this, true);
		mGridView = (YxGridView) findViewById(R.id.noScrollgridview);
		mAdapter = new GridAdapter();
		mGridView.setAdapter(mAdapter);
		
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(mRoot.getContext(), ViewPagerActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra(KEY_EXTRA_IMAGES, mImageUrls);
				intent.putExtra(KEY_EXTRA_INDEX, arg2);
				mRoot.getContext().startActivity(intent);
			}
		});
	}
	
	public void setData(String urls) {
		mImageUrls = urls;
		mCorrectedUrls = initUrls(mImageUrls);
		mAdapter.notifyDataSetChanged();
		if (mCorrectedUrls == null || mCorrectedUrls.size() == 0) {
			this.setVisibility(View.GONE);
		} else {
			this.setVisibility(View.VISIBLE);
		}
	}
	
	public static ArrayList<String> initUrls(String imageUrls) {
		if (UIHelper.isEmpty(imageUrls)) {
			return null;
		}
		String[] urls = imageUrls.split(",");
		ArrayList<String> correctedUrls = new ArrayList<String>();
		for (int i = 0; i < urls.length; i++) {
			if (!UIHelper.isEmpty(urls[i]) && urls[i].startsWith("http")) {
				correctedUrls.add(urls[i]);
			}
		}
		return correctedUrls;
	}
	
	class GridAdapter extends BaseAdapter {
		private static final int MAX = 4;
		ArrayList<String> mData = new ArrayList<String>();

		@Override
		public int getCount() {
			if (mCorrectedUrls == null || mCorrectedUrls.size() == 0) {
				return 0;
			} else {
				return Math.min(mCorrectedUrls.size(), MAX);
			}
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View ret;
			if (arg1 == null) {
				ret = LayoutInflater.from(mRoot.getContext()).inflate(R.layout.item_grid_image_picker, null);
			} else {
				ret = arg1;
			}
			ImageView image = (ImageView) ret.findViewById(R.id.image);
			TextView text = (TextView) ret.findViewById(R.id.text);
			if (mCorrectedUrls.size() > MAX && arg0 == MAX - 1) {
				if (UIHelper.isEmpty(lastImageTipText)) {
					text.setText("共" + mCorrectedUrls.size() + "张图片");
				} else {
					text.setText(lastImageTipText);
				}
				text.setVisibility(View.VISIBLE);
				image.setVisibility(View.INVISIBLE);
//				UIHelper.asyncLoadImage(image, mCorrectedUrls.get(arg0), R.drawable.default_icon);
			} else {
				text.setVisibility(View.INVISIBLE);
				image.setVisibility(View.VISIBLE);
				UIHelper.asyncLoadImage(image, mCorrectedUrls.get(arg0), R.drawable.default_icon);
			}
			return ret;
		}
		
	}

}
